package top.jfunc.fastdfs.happyfish.config;

import org.csource.fastdfs.ClientGlobal;
import org.springframework.boot.context.properties.ConfigurationProperties;
import top.jfunc.fastdfs.happyfish.util.FastdfsUtil;

import java.util.Properties;

/**
 * @author xiongshiyan at 2019/12/5 , contact me with email yanshixiong@126.com or phone 15208384257
 */
@ConfigurationProperties(prefix = "fastdfs")
public class FastdfsConfig {
    public static final FastdfsConfig config = new FastdfsConfig();
    private static final String FASTDFS_URL_PREFIX                 = "fastdfs.url_prefix";

    private int connectTimeoutInSeconds   = ClientGlobal.DEFAULT_CONNECT_TIMEOUT;
    private int networkTimeoutInSeconds   = ClientGlobal.DEFAULT_NETWORK_TIMEOUT;
    private String charset                = ClientGlobal.DEFAULT_CHARSET;
    private boolean httpAntiStealToken    = ClientGlobal.DEFAULT_HTTP_ANTI_STEAL_TOKEN;
    private String httpSecretKey          = ClientGlobal.DEFAULT_HTTP_SECRET_KEY;
    private int httpTrackerHttpPort       = ClientGlobal.DEFAULT_HTTP_TRACKER_HTTP_PORT;

    private String trackerServers;
    private String urlPrefix;

    private ConnectionPool connectionPool = new ConnectionPool();

    public int getConnectTimeoutInSeconds() {
        return connectTimeoutInSeconds;
    }

    public void setConnectTimeoutInSeconds(int connectTimeoutInSeconds) {
        this.connectTimeoutInSeconds = connectTimeoutInSeconds;
    }

    public int getNetworkTimeoutInSeconds() {
        return networkTimeoutInSeconds;
    }

    public void setNetworkTimeoutInSeconds(int networkTimeoutInSeconds) {
        this.networkTimeoutInSeconds = networkTimeoutInSeconds;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public boolean isHttpAntiStealToken() {
        return httpAntiStealToken;
    }

    public void setHttpAntiStealToken(boolean httpAntiStealToken) {
        this.httpAntiStealToken = httpAntiStealToken;
    }

    public String getHttpSecretKey() {
        return httpSecretKey;
    }

    public void setHttpSecretKey(String httpSecretKey) {
        this.httpSecretKey = httpSecretKey;
    }

    public int getHttpTrackerHttpPort() {
        return httpTrackerHttpPort;
    }

    public void setHttpTrackerHttpPort(int httpTrackerHttpPort) {
        this.httpTrackerHttpPort = httpTrackerHttpPort;
    }

    public String getTrackerServers() {
        return trackerServers;
    }

    public void setTrackerServers(String trackerServers) {
        this.trackerServers = trackerServers;
    }

    public String getUrlPrefix() {
        return urlPrefix;
    }

    public void setUrlPrefix(String urlPrefix) {
        this.urlPrefix = urlPrefix;
    }

    public ConnectionPool getConnectionPool() {
        return connectionPool;
    }

    public void setConnectionPool(ConnectionPool connectionPool) {
        this.connectionPool = connectionPool;
    }

    public static class ConnectionPool{
        private boolean enabled       = ClientGlobal.DEFAULT_CONNECTION_POOL_ENABLED;
        private int maxCountPerEntry  = ClientGlobal.DEFAULT_CONNECTION_POOL_MAX_COUNT_PER_ENTRY;
        private int maxIdleTime       = ClientGlobal.DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME;
        private int maxWaitTimeInMs   = ClientGlobal.DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS;

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public int getMaxCountPerEntry() {
            return maxCountPerEntry;
        }

        public void setMaxCountPerEntry(int maxCountPerEntry) {
            this.maxCountPerEntry = maxCountPerEntry;
        }

        public int getMaxIdleTime() {
            return maxIdleTime;
        }

        public void setMaxIdleTime(int maxIdleTime) {
            this.maxIdleTime = maxIdleTime;
        }

        public int getMaxWaitTimeInMs() {
            return maxWaitTimeInMs;
        }

        public void setMaxWaitTimeInMs(int maxWaitTimeInMs) {
            this.maxWaitTimeInMs = maxWaitTimeInMs;
        }
    }

    /**
     * 非 Spring 项目调用此方法初始化config和util
     */
    public static FastdfsConfig initFastdfsConfig(Properties properties) throws Exception{
        if(properties.containsKey(ClientGlobal.PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS)){
            config.connectTimeoutInSeconds = Integer.parseInt(properties.getProperty(ClientGlobal.PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS));
        }
        if(properties.containsKey(ClientGlobal.PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS)){
            config.networkTimeoutInSeconds = Integer.parseInt(properties.getProperty(ClientGlobal.PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS));
        }
        if(properties.containsKey(ClientGlobal.PROP_KEY_CHARSET)){
            config.charset = properties.getProperty(ClientGlobal.PROP_KEY_CHARSET);
        }
        if(properties.containsKey(ClientGlobal.PROP_KEY_HTTP_ANTI_STEAL_TOKEN)){
            config.httpAntiStealToken = Boolean.parseBoolean(properties.getProperty(ClientGlobal.PROP_KEY_HTTP_ANTI_STEAL_TOKEN));
        }
        if(properties.containsKey(ClientGlobal.PROP_KEY_HTTP_SECRET_KEY)){
            config.httpSecretKey = properties.getProperty(ClientGlobal.PROP_KEY_HTTP_SECRET_KEY);
        }
        if(properties.containsKey(ClientGlobal.PROP_KEY_HTTP_TRACKER_HTTP_PORT)){
            config.httpTrackerHttpPort = Integer.parseInt(properties.getProperty(ClientGlobal.PROP_KEY_HTTP_TRACKER_HTTP_PORT));
        }
        config.trackerServers                = properties.getProperty(ClientGlobal.PROP_KEY_TRACKER_SERVERS);
        config.urlPrefix                     = properties.getProperty(FASTDFS_URL_PREFIX);


        //连接池相关
        if(properties.containsKey(ClientGlobal.PROP_KEY_CONNECTION_POOL_ENABLED)){
            config.connectionPool.enabled = Boolean.parseBoolean(properties.getProperty(ClientGlobal.PROP_KEY_CONNECTION_POOL_ENABLED));
        }
        if(properties.containsKey(ClientGlobal.PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY)){
            config.connectionPool.maxCountPerEntry = Integer.parseInt(properties.getProperty(ClientGlobal.PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY));
        }
        if(properties.containsKey(ClientGlobal.PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME)){
            config.connectionPool.maxIdleTime = Integer.parseInt(properties.getProperty(ClientGlobal.PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME));
        }
        if(properties.containsKey(ClientGlobal.PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS)){
            config.connectionPool.maxWaitTimeInMs = Integer.parseInt(properties.getProperty(ClientGlobal.PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS));
        }


        //使FastdfsUtil可用
        FastdfsUtil.init(config);

        return config;
    }
}